--[[
* MapNotes 2.0
* File: MapNotes\MapNotes_EditMenu.lua
* $Revision: 138 $
* $Date: 2007-01-12 23:04:42 +0000 (Fri, 12 Jan 2007) $
* 
* Copyright (c) 2006-2007, Mery (Moritz Metelmann)
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* 
*    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*    * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]]--

if (not MapNotes) then MapNotes = {}; end;
if (not MapNotes.EditMenu) then MapNotes.EditMenu = {}; end;
if (not MapNotes.EditMenu.IconDropDown) then MapNotes.EditMenu.IconDropDown = {}; end;
if (not MapNotes.EditMenu.CategoryDropDown) then MapNotes.EditMenu.CategoryDropDown = {}; end;

local L = MapNotes.Localization;

MapNotes.Colors = {
	{r=1   ,g=1   ,b=1   }, -- white
	{r=0   ,g=0   ,b=0   }, -- black
};

MapNotes.EditMenu.IconDropDown.Toggle = function()
	if ( not MapNotes.Icon.DropDown.IsVisible() ) then
		MapNotes.EditMenu.ClearFocus_CloseDropDown("icondropdown");
		MapNotes.Icon.DropDown.Show(noHighlight, 1, MapNotes.EditMenu.IconDropDown.SetIcon, MapNotesEditMenuFrame, MAPNOTES_FRAMELEVEL_EDITMENU+3, "TOPLEFT", MapNotesEditMenuIconDropDown, "TOPLEFT", 12, -22);
	else
		MapNotes.Icon.DropDown.Hide();
	end;
end;

MapNotes.EditMenu.IconDropDown.SetIcon = function(iconID)
	MapNotes.EditMenu.iconID = iconID;
	MapNotes.EditMenu.IconDropDown.Update();
	MapNotes.EditMenu.ClearFocus_CloseDropDown();
end;

MapNotes.EditMenu.IconDropDown.Update = function()
	local icon, texture;
	if (MapNotes.EditMenu.iconID == "DEFAULT") then
		-- set text default
		MapNotesEditMenuIconDropDownText:SetText(L["Default Icon"]);
		-- set texture to texture
		icon = MapNotes.Category.GetDefaultIconID(MapNotes.EditMenu.categoryName);
	else
		-- set text to custom
		MapNotesEditMenuIconDropDownText:SetText(L["Custom Icon"]);
		-- set texture to texture
		icon = MapNotes.EditMenu.iconID;
	end;
	icon = MapNotes.Icon.GetIconTexture(icon);
	texture = MapNotesEditMenuIconDropDownIconTexture;
	texture:SetTexture(icon.file);
	if (icon.size < 24) then
		texture:SetWidth(icon.size); texture:SetHeight(icon.size);
	else
		texture:SetWidth(24); texture:SetHeight(24);
	end;
	if (icon.tcoordMinX) then texture:SetTexCoord(icon.tcoordMinX, icon.tcoordMaxX, icon.tcoordMinY, icon.tcoordMaxY); else texture:SetTexCoord(0, 1, 0, 1); end;
	if (icon.colorR) then texture:SetVertexColor(icon.colorR, icon.colorG, icon.colorB, icon.colorA); else texture:SetVertexColor(1, 1, 1, 1); end;
end;

MapNotes.EditMenu.CategoryDropDown.SetCategory = function(name)
	MapNotes.EditMenu.categoryName = name;
	MapNotes.EditMenu.CategoryDropDown.Update();
	MapNotes.EditMenu.IconDropDown.Update();
	MapNotes.EditMenu.ClearFocus_CloseDropDown();
end;

MapNotes.EditMenu.CategoryDropDown.Update = function()
	MapNotesEditMenuCategoryDropDownText:SetText(MapNotes.EditMenu.categoryName);
end;

MapNotes.EditMenu.CategoryDropDown.Toggle = function()
	if ( not MapNotes.Category.DropDown.IsVisible() ) then
		MapNotes.EditMenu.ClearFocus_CloseDropDown("categorydropdown");
		MapNotes.Category.DropDown.Show(noHighlight, 1, MapNotes.EditMenu.CategoryDropDown.SetCategory, MapNotesEditMenuFrame, MAPNOTES_FRAMELEVEL_EDITMENU+3,"TOPLEFT", MapNotesEditMenuCategoryDropDown, "TOPLEFT", 12, -22);
	else
		MapNotes.Category.DropDown.Hide();
	end;
end;

MapNotes.EditMenu.ClearFocus_CloseDropDown = function(exception)
	if (exception ~= "title") then MapNotesEditMenuTitleEditBox:ClearFocus(); end;
	if (exception ~= "scrollframe") then MapNotesEditMenuScrollFrameText:ClearFocus(); end;
	if (exception ~= "icondropdown") then MapNotes.Icon.DropDown.Hide(); end;
	if (exception ~= "categorydropdown") then MapNotes.Category.DropDown.Hide(); end;
	if (exception ~= "colorpicker") then end;
end;

MapNotes.EditMenu.OnShow = function()
	MapLib.Block.Request("MapNotesEditMenu", MapNotes.EditMenu.ClearFocus_CloseDropDown, MAPNOTES_FRAMELEVEL_EDITMENU-1);
end;

MapNotes.EditMenu.OnHide = function()
	MapLib.Block.Release("MapNotesEditMenu");
end;

MapNotes.EditMenu.SetTitleColor = function(colorIndex)
	MapNotes.EditMenu.titleColor = colorIndex;
	-- update the button that's yet to be implemented
end;

MapNotes.EditMenu.SetDescriptionColor = function(colorIndex)
	MapNotes.EditMenu.descriptionColor = colorIndex;
	-- update the button that's yet to be implemented
end;

MapNotes.EditMenu.NewNote = function(zone, x, y)
	MapNotes.EditMenu.zone = zone;
	MapNotes.EditMenu.x = x;
	MapNotes.EditMenu.y = y;
	MapNotes.EditMenu.type = "NEW";
	-- clear title and scrollframe
	MapNotesEditMenuTitleEditBox:SetText("");
	MapNotesEditMenuScrollFrameText:SetText("");
	MapNotes.EditMenu.SetTitleColor(1);
	MapNotes.EditMenu.SetDescriptionColor(1);
	-- set category and icon
	MapNotes.EditMenu.CategoryDropDown.SetCategory(MapNotes.Category.generalCategory.name);
	MapNotes.EditMenu.IconDropDown.SetIcon("DEFAULT");
	MapNotesEditMenuFrame:Show();
end;

MapNotes.EditMenu.EditNote = function(poi)
	local note = MapNotesSaved_NoteDB[poi.id];
	MapNotes.EditMenu.type = "EDIT";
	MapNotes.EditMenu.noteID = poi.id;
	-- clear title and scrollframe
	MapNotesEditMenuTitleEditBox:SetText(note.title);
	if (note.description) then MapNotesEditMenuScrollFrameText:SetText(note.description); else MapNotesEditMenuScrollFrameText:SetText(""); end;
	local _, _, titleColor, descriptionColor, _, _, _ = string.find(note.misc, "^([^%s]+)\$([^%s]+)\$([^%s]+)\$([^%s]+)\$([^%s]+)\$");
	MapNotes.EditMenu.SetTitleColor(titleColor);
	MapNotes.EditMenu.SetDescriptionColor(descriptionColor);
	-- set category and icon
	MapNotes.EditMenu.CategoryDropDown.SetCategory(note.category);
	MapNotes.EditMenu.IconDropDown.SetIcon(note.iconID);
	MapNotesEditMenuFrame:Show();
end;

MapNotes.EditMenu.DeleteNote = function(poi)
	MapNotes.DeleteNote(poi.id);
end;

MapNotes.EditMenu.OnSaveClick = function()
	if (MapNotes.EditMenu.type == "NEW") then
		MapNotes.CreateNote(MapNotes.EditMenu.zone, MapNotes.EditMenu.x, MapNotes.EditMenu.y, MapNotesEditMenuTitleEditBox:GetText(), MapNotes.EditMenu.titleColor, MapNotes.EditMenu.categoryName, MapNotes.EditMenu.iconID, MapNotesEditMenuScrollFrameText:GetText(), MapNotes.EditMenu.descriptionColor);
	elseif (MapNotes.EditMenu.type == "EDIT") then
		MapNotes.UpdateNote(MapNotes.EditMenu.noteID, MapNotesEditMenuTitleEditBox:GetText(), MapNotes.EditMenu.titleColor, MapNotes.EditMenu.categoryName, MapNotes.EditMenu.iconID, MapNotesEditMenuScrollFrameText:GetText(), MapNotes.EditMenu.descriptionColor);
	end;
	MapNotesEditMenuFrame:Hide();
end;

MapNotes.EditMenu.OnCancelClick = function()
	MapNotesEditMenuFrame:Hide();
end;